angular.module('start.user.ordervisitor.ctrl', [])

  .controller('ordervisitorCtrl', function($scope, $timeout, $ionicScrollDelegate, $$UserOrderService, $rootScope, $location, $ionicLoading) {
    console.log("ordervisitorCtrl");

    $scope.tabStatus = 1;

    $scope.visitororders = [];
    $rootScope.orderDetail = {};

    $scope.tabStatusChange = function(index) {
      $scope.tabStatus = index;
      $ionicScrollDelegate.scrollTop(0);
    };


    $scope.getUserOrdersList = function() {
      $ionicLoading.show({
        template: '<ion-spinner icon="ios"></ion-spinner>',
        noBackdrop: true
      });
      // 获取用户的订单列表
      $$UserOrderService.getUserOrders()
        .then(function(res) {
          $scope.visitororders = res.data.list;
          $ionicLoading.hide();
        });
    };
    $scope.getUserOrdersList();

    // 用户取消预约
    $scope.userCancelOrder = function(orderId, state) {
      $ionicLoading.show({
        template: '<ion-spinner icon="ios"></ion-spinner>',
        noBackdrop: true
      });
      $$UserOrderService.userCancelOrder(orderId, state)
        .then(function(res) {
          $ionicLoading.show({
            template: res.msg,
            noBackdrop: true,
            duration: 1500,
            duration: 1500
          });
          $timeout(function() {
            $scope.getUserOrdersList();
          }, 1500);
        });
    };

    // 用户删除订单
    $scope.userdeleteOrder = function(orderId, state) {
      $ionicLoading.show({
        template: '<ion-spinner icon="ios"></ion-spinner>',
        noBackdrop: true
      });
      $$UserOrderService.deleteOrder(orderId, state)
        .then(function(res) {
          $ionicLoading.show({
            template: res.msg,
            noBackdrop: true,
            duration: 1500,
            duration: 1500
          });
          $timeout(function() {
            $scope.getUserOrdersList();
          }, 1500);
        });
    };




    // 游客确认完成服务 103 -> 104
    $scope.visitorSureServiceOrder = function(orderId, state) {
      $ionicLoading.show({
        template: '<ion-spinner icon="ios"></ion-spinner>',
        noBackdrop: true
      });
      $$UserOrderService.changeOrderState(orderId, state)
        .then(function(res) {
          $ionicLoading.show({
            template: res.msg,
            noBackdrop: true,
            duration: 1500
          });
          $timeout(function() {
            $scope.getUserOrdersList();
          }, 1500);
        });
    };

    // 去评论订单
    $scope.commentOrder = function(index) {
      $rootScope.orderDetail = $scope.visitororders[index];
      $location.path("ordercomment/" + $rootScope.orderDetail.orderId);
    };

    // 去投诉订单
    $scope.complaintOrder = function(index) {
      $rootScope.orderDetail = $scope.visitororders[index];
      $location.path("ordercomplaint/" + $rootScope.orderDetail.orderId);
    };

    // 查看订单评论
    $scope.orderCOmmentDetail = function(argument) {
      // $location.path("ordercomment/"+orderId);
    };

    // 用户去支付订单
    $scope.gopaypage = function(index) {
      // 用户去支付订单
      $rootScope.orderDetail = $scope.visitororders[index];
      $location.path("orderpay/" + $rootScope.orderDetail.orderId);
    };

    // 查看订单详情页面
    $scope.goorderdetail = function(index) {
      $rootScope.orderDetail = $scope.visitororders[index];
      $location.path("orderdetailvisitor/" + $rootScope.orderDetail.orderId);
    };

    $scope.calcdateDays = function(start, end) {
      return parseInt((new Date(end).getTime() - new　 Date(start).getTime()) / (86400000));
    };


  });
